﻿using Microsoft.Extensions.Hosting;
using System;
using System.IO;
using Dralee.ConsulRegister.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NLog.Extensions.Logging;
using System.Threading.Tasks;
using Dralee.ConsulRegister.Services;

namespace Dralee.ConsulRegister
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var builder = new HostBuilder()
                .ConfigureLogging(factory =>
                {
                    factory.AddNLog(new NLogProviderOptions
                    { CaptureMessageTemplates = true, CaptureMessageProperties = true });
                    NLog.LogManager.LoadConfiguration("nlog.config");
                })
                .ConfigureHostConfiguration(config =>
                {
                        //.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
                    if (args != null)
                    {
                        config.AddCommandLine(args);
                    }
                })
                .ConfigureAppConfiguration((hostContext, config) =>
                {
                    config.SetBasePath(Directory.GetCurrentDirectory());
                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                    config.AddEnvironmentVariables();

                    if (args != null)
                    {
                        config.AddCommandLine(args);
                    }
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddOptions();//.Configure<ConsulService>(hostContext.Configuration.GetSection(""));
                    
                    //services.Configure<ConsulService>(hostContext.Configuration.GetSection("ConsulService"));
                    services.Configure<ConsulService>(hostContext.Configuration.GetSection("Consul"));
                    services.Configure<HealthService>(hostContext.Configuration.GetSection("Services"));

                    // services
                    services.AddHostedService<RegisterServices>();

                });

            //builder.RegisterConsul();
            //builder.Build().
            await builder.RunConsoleAsync();
            //await builder.RunConsoleAsync();
        }
    }
}
